package com.idcf.boathouse.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import com.idcf.boathouse.order.dao.BoatHouseProductDao;
import com.idcf.boathouse.order.dao.BoatHouseSetDao;
import com.idcf.boathouse.order.dao.FoodDao;
import com.idcf.boathouse.order.dao.OrderDao;
import com.idcf.boathouse.order.entity.BoatHouseProduct;
import com.idcf.boathouse.order.entity.BoatHouseSet;
import com.idcf.boathouse.order.entity.FoodPo;
import com.idcf.boathouse.order.entity.OrderPo;
import com.idcf.boathouse.order.service.OrderService;
import com.idcf.boathouse.order.utils.DateUtil;
import com.idcf.boathouse.order.vo.FoodVo;
import com.idcf.boathouse.order.vo.OrderVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description
 * @author: steve chenwang94@gmail.com
 * @create: 2020-12-29 14:23:23
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderDao orderDao;
    @Resource
    FoodDao foodDao;
    @Resource
    BoatHouseSetDao boatHouseSetDao;
    @Resource
    BoatHouseProductDao boatHouseProductDao;

    @Override
    public List<OrderVo> getOrderList() {
        List<OrderPo> orderPos = orderDao.queryAll();
        if (null == orderPos || orderPos.isEmpty()) {
            return new ArrayList<>();
        }
        List<OrderVo> orderVos = new ArrayList<>();
        for (OrderPo orderPo : orderPos) {
            OrderVo orderVo = new OrderVo();
            BeanUtil.copyProperties(orderPo, orderVo);
            orderVos.add(orderVo);
        }
        List<String> orderIds = orderPos.stream().map(OrderPo::getOrderId).collect(Collectors.toList());
        List<FoodPo> foodPos = foodDao.findFoodByOrderIds(orderIds);
        List<FoodVo> foodVos = new ArrayList<>();
        for (FoodPo foodPo : foodPos) {
            FoodVo foodVo = new FoodVo();
            BeanUtil.copyProperties(foodPo, foodVo);
            foodVos.add(foodVo);
        }
        Map<String, List<FoodVo>> foodMap = foodVos.stream().collect(Collectors.groupingBy(FoodVo::getOrderId));
        for (OrderVo orderVo : orderVos) {
            orderVo.setItemsList(foodMap.get(orderVo.getOrderId()));
        }
        return orderVos;
    }

    @Override
    public void confirmOrder(String orderId) {
        OrderPo orderPo = orderDao.queryByOrderId(orderId);
        orderPo.setOrderStatus(1);
        orderPo.setOrderStatusDesc("待配送");
        orderPo.setUpdateTime(DateUtil.getDateTime());
        orderDao.update(orderPo);
    }

    @Override
    public void refuseOrder(String orderId) {
        OrderPo orderPo = orderDao.queryByOrderId(orderId);
        orderPo.setOrderStatus(2);
        orderPo.setOrderStatusDesc("商家拒绝订单");
        orderPo.setUpdateTime(DateUtil.getDateTime());
        orderDao.update(orderPo);
    }

    @Override
    public void deliverOrder(String orderId) {
        OrderPo orderPo = orderDao.queryByOrderId(orderId);
        orderPo.setOrderStatus(3);
        orderPo.setOrderStatusDesc("配送员：小王 正在为您送餐~");
        orderPo.setUpdateTime(DateUtil.getDateTime());
        orderDao.update(orderPo);
    }

    @Override
    //@Transactional
    public void makeNewOrder(Integer setId, String address) {
        BoatHouseSet boatHouseSet = boatHouseSetDao.queryById(setId);
        String totalAmount = boatHouseSet.getSetAmount();
        String orderId = UUID.fastUUID().toString();
        OrderPo orderPo = new OrderPo();
        orderPo.setOrderId(orderId)
                .setOrderTime(DateUtil.getDateTime())
                .setCreateTime(DateUtil.getDateTime())
                .setUpdateTime(DateUtil.getDateTime())
                .setNote(address)
                .setOrderStatus(0)
                .setOrderStatusDesc("待接单")
                .setPayType(0)
                .setUserId(1)
                .setTotalAmount(totalAmount);
        orderDao.insert(orderPo);
        // 写food（detail）表
        List<Integer> productIds = Arrays.stream(boatHouseSet.getProducts().split(",")).map(Integer::valueOf).collect(Collectors.toList());
        List<BoatHouseProduct> boatHouseProducts = boatHouseProductDao.queryByIds(productIds);
        for (BoatHouseProduct boatHouseProduct : boatHouseProducts) {
            FoodPo foodPo = new FoodPo();
            foodPo.setFoodId(boatHouseProduct.getId());
            foodPo.setFoodName(boatHouseProduct.getFoodName());
            foodPo.setFoodNum(1);
            foodPo.setFoodPrice(boatHouseProduct.getFoodPrice());
            foodPo.setFoodSubTotal(boatHouseProduct.getFoodPrice());
            foodPo.setOrderId(orderId);
            foodPo.setFoodPicture(boatHouseProduct.getFoodPicture());
            foodDao.insert(foodPo);
        }
    }
}
